using System;
using System.IO;
using System.Windows.Forms;
using System.Collections;

namespace Patchy
{
	/// <summary>
	/// Summary description for CreateBatchFiles.
	/// </summary>
	public class CreateBatchFiles : CreateFile
	{
		string mInstallFile;
		string mUninstallFile;
        GroupHolder mGroupHolder;
		PatchDetails mDetails;

		public CreateBatchFiles(){}

        public override void Init(PatchDetails mDetails, GroupHolder myGroups)
		{
			mInstallFile = "Install " + mDetails.PatchBaseFileName + ".bat";
			mUninstallFile = "Uninstall " + mDetails.PatchBaseFileName + ".bat";
			mGroupHolder = myGroups;
			this.mDetails = mDetails;
		}
		
		public override string Write(string directoryName)
        {
            string report = "";
			TextWriter instTw = null;
			string installPath = Path.Combine(directoryName, mInstallFile);
			string uninstallPath = Path.Combine(directoryName, mUninstallFile);
			
			try
			{
				instTw = new StreamWriter(installPath);
				BatchInstall bt = new BatchInstall(mDetails, mGroupHolder);
				instTw.Write(bt.ToString());
                report += string.Format("Successfully created:\n{0}\n", installPath);
			}
			catch(Exception ex)
            {
                report += string.Format("Failed to write to:\n{0}\nError is: {1}", installPath, ex.Message);
			}
			finally
			{
				if(instTw != null)
				{
					instTw.Close();
				}
			}

			try
			{
				instTw = new StreamWriter(uninstallPath);
				BatchUninstall bu = new BatchUninstall(mDetails, mGroupHolder);
                instTw.Write(bu.ToString());
                report += string.Format("Successfully created:\n{0}\n", uninstallPath);
			}
			catch(Exception ex)
            {
                report += string.Format("Failed to write to:\n{0}\nError is: {1}", uninstallPath, ex.Message);
			}
			finally
			{
				if(instTw != null)
				{
					instTw.Close();
				}
			}
            return report;
        }

        public override bool RequestsCopy()
        {
            return true;
        }
	}
}
